/** 
 * @jsName: FinOpecompanyBillManaged.js 
 * @author Sean Cui
 * @date 2017-04-16 下午3:46:00 
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @version V1.0.0.0
 * @comment 该文件通过代码生成器生成，如果修改次文件后，重新生成时将覆盖修改内容，请修改生成模板或做好备份
 */

var FinOpecompanyBillManaged = function() {
	return {
		datatable : null,
		init : function() {
			$('#countMonth').datetimepicker({
        		lang:'ch',
        		timepicker:false,
        		format:'Y-m'
        	});
			this.initSearch();
			this.initListener();
		},
		initSearch : function() {
			if (!jQuery().dataTable) {
				return;
			}
			var listTable = $("#FinOpecompanyBillListTable").dataTable({
				"sAjaxSource" : ctx + "/finOpecompanyBill/search?now=" + new Date().getTime(),
				/* 使用post方式 */
				"fnServerData" : function(sSource, aoData, fnCallback) {
					var postData = {
						page : 1,
						start : 0,
						rows : 10,
						params : {}
					};
					
					if($('#companyName').val()){
						postData.params.companyName = "%"+($('#companyName').val()).replace(/\s/g,"%")+"%";
					}
					if($('#countMonth').val()){
						postData.params.countMonth = ($('#countMonth').val());
					}
					if($('#state').val()){
						postData.params.state = ($('#state').val());
					}

					// 参数
					for (var i = 0, size = aoData.length; i < size; i++) {
						if (aoData[i].name == 'iDisplayLength') {
							postData.rows = aoData[i].value;
						} else if (aoData[i].name == 'iDisplayStart') {
							postData.start = aoData[i].value;
						} else if (aoData[i].name == 'sEcho') {
							postData.page = aoData[i].value;
						}
					}

					$.ajax({
						"dataType" : 'json',
						"type" : "POST",
						"cache": false,
						"url" : sSource,
						"data" : postData,
						"success" : function(data, textStatus, jqXHR){
							fnCallback(data.data, textStatus, jqXHR);
						}
					});
				},
				"bAutoWidth":false,
				"aoColumnDefs" : [
						{
							"data" : "index",
							"aTargets" : [ 0 ]
						},
						{
							"data" : "companyName",
							"aTargets" : [ 1 ]
						},
						{
							"data" : "allAmount",
							"aTargets" : [ 2 ]
						},
						{
							"data" : "procedFee",
							"aTargets" : [ 3 ]
						},
						{
							"data" : "inscompanyAmount",
							"aTargets" : [ 4 ]
						},
						{
							"data" : "headofficeAmount",
							"aTargets" : [ 5 ]
						},
						{
							"data" : "filialeAmount",
							"aTargets" : [ 6 ]
						},
						{
							"data" : "realAmount",
							"aTargets" : [ 7 ]
						},
						{
							"data" : "countMonth",
							"aTargets" : [ 8 ]
						},
						{
							"data" : "state",
							"aTargets" : [ 9 ]
						},
						{
							"aTargets" : [ 10 ],
							"mRender" : function(data, type, full) {
								var btns =  '';
								btns +=  Permission.createPermElement('finOpecompanyBill:list:modify',' <button title="'+messages.public_button_edit+'" class="btn btn-sm btn-primary" name="edit" target_id="'+full.id+'"><i class="fa fa-pencil-square-o"></i></button>');
								btns +=  Permission.createPermElement('finOpecompanyBill:list:del',' <button title="'+messages.public_button_del+'" href="javascript:;" class="btn btn-sm  btn-danger" name="del" target_id="'+full.id+'"><i class="fa fa-times"></i></button>');
								return btns;
							}
						}
				]
			});
			this.datatable = listTable;
		},reSearch:function(){
        	this.datatable.fnDraw();
        },removeClass:function(){//去除Form表单的验证样式
        	$('.form-group','#actForm').removeClass('has-success')
            .removeClass('has-error');
    		 $('span').remove('.help-block');
        },initListener : function() {
			/*
			 * 查询
			 */
			$("#searchBtn").click(function() {
				FinOpecompanyBillManaged.reSearch();
				return false;
			});

			/*
			 * 清除
			 */
			$("#clearBtn").click(function() {
				$("#searchForm")[0].reset();
				return false;
			});

			/*
			 * 添加
			 */
			$("#addBtn").click(function() {
				FinOpecompanyBillManaged.removeClass();
				$("div[name=editDlg] h4.modal-title").html("新增");
				$("#actForm")[0].reset();
				$("#actForm").find("input[name='id']").val("");
				$("#editDlg").modal("show");
			});
			
			$("#actFormBtn").click(function(){
				var form1 = $('#actForm');
				form1.validate({
					rules : {
						companyName:{
							maxlength : 25,
							required : true
						},
						allAmount:{
							maxlength : 25,
							required : true
						},
						procedFee:{
							maxlength : 25,
							required : true
						},
						inscompanyAmount:{
							maxlength : 25,
							required : true
						},
						headofficeAmount:{
							maxlength : 25,
							required : true
						},
						filialeAmount:{
							maxlength : 25,
							required : true
						},
						realAmount:{
							maxlength : 25,
							required : true
						},
						countMonth:{
							maxlength : 25,
							required : true
						},
						state:{
							maxlength : 25,
							required : true
						},
						version:{
							maxlength : 25,
							required : true
						}
					}
				});
				if(form1.valid()){
					var postData = $("form[name='actForm']").serializeJson();
					$.ajax({
						"dataType" : 'json',
						"cache": false,
						"type" : "POST",
						"data" : postData,
						"url" : ctx + "/finOpecompanyBill/edit?now="+new Date().getTime(),
						"success" : function(data) {
							if(data.type=='success'){
								$("#editDlg").modal("hide");
								BootstrapDialog.show({
									title : messages.public_cue_cue,
									message : messages.public_cue_saveOk,
									buttons : [ {
										label : messages.public_cue_sure,
										cssClass : 'btn btn-sm btn-primary',
										action : function(dialogItself) {
											FinOpecompanyBillManaged.reSearch();
											dialogItself.close();
										}
									} ]
								});
								
							}else{
								BootstrapDialog.show({
									type : BootstrapDialog.TYPE_WARNING,
									title : messages.public_cue_warning,
									message : data.message,
									buttons : [ {
										label : messages.public_cue_sure,
										cssClass : 'btn btn-sm btn-primary',
										action : function(dialogItself) {
											FinOpecompanyBillManaged.reSearch();
											dialogItself.close();
										}
									} ]
								});
							}
						}
					});
				}
			});
			
			/*
			 * 编辑
			 */
			$('#FinOpecompanyBillListTable tbody')
				.on('click', 'button[name="edit"]',function() {
					var id = $(this).attr("target_id");
					var form=$("form[name='actForm']");
					FinOpecompanyBillManaged.removeClass();
					$.ajax({
						"dataType" : 'json',
						"cache" : false,
						"type" : "POST",
						"data" : {},
						"url" : ctx + '/finOpecompanyBill/get/'+id+"?now="+new Date().getTime(),
						"success" : function(data, textStatus, jqXHR){
							if (data.type == 'success') {
								$(form).binddata(data.data);
								$("div[name=editDlg] h4.modal-title").html("编辑角色");
								$("#editDlg").modal("show");
							} else {
								BootstrapDialog.show({
									title : messages.public_cue_message,
									message : data.message,
									buttons : [ {
										label : messages.public_cue_sure,
										action : function(dialog) {
											dialog.close();
										}
									} ]
								});
							}
						}
					});
				});
			$('#FinOpecompanyBillListTable tbody')
				.on('click', 'button[name="del"]',function() {
					var id = $(this).attr("target_id");
					FinOpecompanyBillManaged.del(id);
				});
		},
		/**
		 * 删除
		 */
		del : function(id) {
			BootstrapDialog.show({
				type : BootstrapDialog.TYPE_WARNING,
				title : messages.public_cue_warning,
				message : messages.public_cue_delSure,
				buttons : [ {
					label : messages.public_cue_close,
					cssClass: 'btn btn-sm btn-default',
					action : function(dialogItself) {
						dialogItself.close();
					}
				}, {
					label : messages.public_cue_sure,
					cssClass : 'btn btn-sm btn-primary',
					action : function(dialogItself) {
						FinOpecompanyBillManaged.sureDel(id);
						dialogItself.close();
					}
				} ]
			});
		},
		sureDel : function(id){
			$.ajax({
				"dataType" : 'json',
				"cache": false,
				"type" : "POST",
				"data" : {},
				"url" : ctx + '/finOpecompanyBill/delete/'+id+"?now="+new Date().getTime(),
				"success" : function(data, textStatus, jqXHR){
					if (data.type == 'success') {
						BootstrapDialog.show({
							title : messages.public_cue_message,
							message : messages.public_cue_delSuccess,
							buttons : [ {
								label : messages.public_cue_sure,
								action : function(dialog) {
									FinOpecompanyBillManaged.reSearch();
									dialog.close();
								}
							} ]
						});
					} else {
						BootstrapDialog.show({
							title : messages.public_cue_message,
							message : data.message,
							buttons : [ {
								label : messages.public_cue_sure,
								action : function(dialog) {
									dialog.close();
								}
							} ]
						});
					}
				}
			});
		}
	};

}();

/*
 * Powered By [Code-Generator]
 * Web Site: http://www.hwelltech.com
 */

